package com.qf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/27 16:01
 *
 *
 * 商品类目表
 */
@CrossOrigin("*")
@RequestMapping("/category")
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param id
     * @param name
     * @return
     */
    @GetMapping("/list")
    public  Object catePage(Integer page, Integer limit, String sort, String order, Integer id, String name){

        //创建查询条件
        IPage<DtsCategory> iPage = categoryService.queryByCategory(page, limit, sort, order, id, name);

        //查询总条数
        long total = iPage.getTotal();

        //查询每页页数
        List<DtsCategory> list = iPage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", list);
        return ResponseUtil.ok(data);


    }


    /**
     * 添加
     * @param category
     * @return
     */
    @RequestMapping("/create")
    public Object add(@RequestBody DtsCategory category){
        DtsCategory add = categoryService.add(category);
        return  ResponseUtil.ok(add);
    }

    /**
     * 删除
     * @param category
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody  DtsCategory category){
        categoryService.delete(category);
        return  ResponseUtil.ok();
    }


    /**
     * 修改
     * @param category
     * @return
     */
    @PostMapping("/update")
    public  Object update(@RequestBody DtsCategory category){
        int update = categoryService.update(category);
        return  ResponseUtil.ok(update);
    }

    /**
     * 商品类目
     * @return
     */
    @GetMapping("/l1")
    public  Object findCat(){
        List<CatVo> cat = categoryService.findCat();
        return ResponseUtil.ok(cat);
    }
}
